
package combat.dudes.client.gui.threads.map;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import combat.dudes.client.ClientApp;
import combat.dudes.client.gui.threads.map.ClientGUIMap.MapType;
import combat.dudes.protocol.data.PhysicalObject;
import combat.dudes.protocol.interfaces.OnboardWeaponSystemInt;
import combat.dudes.protocol.interfaces.PhysicalObjectInt;
import combat.dudes.protocol.interfaces.ProjectileInt;


/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class GUIMapProjectile extends JPanel {
	private static final long serialVersionUID = 1L;


	public static int g2W = 300, g2Whalf = g2W / 2;
	public static int g2H = 100, g2Hhalf = g2H / 2;

	private BufferedImage image = null;
	public synchronized BufferedImage getImage() {
		return image;
	}

	private ProjectileInt projectile = null;
	private Standing standing = Standing.UNKNOWN;


	public GUIMapProjectile(MapType type, final ProjectileInt projectile) {
		this.projectile = projectile;

		if(ClientApp.SharedVars.getMyTeam() != null)
			for(ProjectileInt p : ClientApp.SharedVars.getMyTeam().getProjectiles())
					this.standing = (projectile.getObjID() == p.getObjID()) ? Standing.FRIENDLY : Standing.HOSTILE;

		setBounds(0, 0, g2W, g2H);

		// http://www.java.net/node/683925
		image = new BufferedImage(g2W + 1, g2H + 1, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = (Graphics2D) image.getGraphics();
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on for shapes
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // Set AA off for text
		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Set render focus on speed
		// Transparent background
		g2.setBackground(new Color(0, 0, 0, 0));
		g2.clearRect(0, 0, image.getWidth(), image.getHeight());

		// Draw correct icon
		g2.drawImage(drawShape(), null, g2Whalf - 10, g2Hhalf - 10);

		// Construct dot
		// g2.setColor(Color.WHITE);
		// g2.drawOval(g2Whalf - 1, g2Hhalf, 1, 1);

		// Construct line of speed
		// double angle = Math.toRadians(vehicle.getDirection());
		// g2.drawLine(g2Whalf, g2Hhalf, (int) Math.floor(g2Whalf + vehicle.getSpeed() * Math.cos(-angle)), (int) Math.floor(g2Hhalf + vehicle.getSpeed() * Math.sin(-angle)));

		// Paint Vehicle name
		g2.drawString(projectile.getName(), g2Whalf + 20, g2Hhalf + (g2.getFontMetrics().getHeight() / 2));


		if(type == MapType.MAIN) {
			JPanel mousePanel = new JPanel() {
				private static final long serialVersionUID = 1L;
				@Override
				public void paintComponent(Graphics g) {
					Graphics2D g2 = (Graphics2D) image.getGraphics();
					// Transparent background
					g2.setBackground(new Color(0, 0, 0, 0));
					g2.clearRect(0, 0, getWidth(), getHeight());
				}
			};
			mousePanel.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent m) {
					if(SwingUtilities.isLeftMouseButton(m)) {
						ClientApp.SharedVars.setSelectedItemOnMap((PhysicalObjectInt) projectile);
						ClientApp.Log("Clicked (left) on projectile '" + projectile.getName() + "'");
					}
					if(SwingUtilities.isRightMouseButton(m)) {
						ClientApp.SharedVars.setSelectedItemOnMap((PhysicalObject) projectile);
						ClientApp.SharedVars.getMyVehicle().orderAttack(0, projectile, (OnboardWeaponSystemInt) ClientApp.SharedVars.getMyVehicle().getOnboardSystems().get(0));
						ClientApp.Log("Clicked (right) on projectile '" + projectile.getName() + "'");
					}
				}
			});
			mousePanel.setBounds(0, 0, g2W, g2H);
			this.add(mousePanel, JLayeredPane.POPUP_LAYER);
		}

	}
	private int shapeW = 20, shapeWhalf = shapeW / 2;
	private int shapeH = 18, shapeHhalf = shapeH / 2;


	private BufferedImage drawShape() {
		// http://en.wikipedia.org/wiki/NATO_Military_Symbols_for_Land_Based_Systems#Affiliation
		BufferedImage shape = new BufferedImage(21, 21, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = (Graphics2D) image.getGraphics();
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on for shapes
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // Set AA off for text
		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Set render focus on speed
		// Transparent background
		g2.setBackground(new Color(0, 0, 0, 0));
		g2.clearRect(0, 0, image.getWidth(), image.getHeight());

		g2.setColor(new Color(255, 0, 0));
		int rX[] = { g2Whalf - shapeWhalf / 2, g2Whalf, g2Whalf + shapeWhalf / 2 };
		int rY[] = { g2Hhalf + shapeHhalf / 2, g2Hhalf - shapeHhalf / 2, g2Hhalf + shapeHhalf / 2 };
		g2.drawPolygon(rX, rY, rX.length);

		// g2.fillRect(g2Whalf - shapeWhalf, g2Hhalf - shapeHhalf, shapeW, shapeH);

		/* switch(this.standing) {
		 * case FRIENDLY:
		 * g2.setColor(new Color(31, 97, 239)); // Blue
		 * if(GUIMapLand.isOnLand(this.projectile.getPosition()))
		 * g2.drawRect(g2Whalf - shapeWhalf, g2Hhalf - shapeHhalf, shapeW, shapeH);
		 * else g2.drawOval(g2Whalf - shapeWhalf, g2Hhalf - shapeHhalf, shapeW, shapeH);
		 * break;
		 * case NEUTRAL:
		 * g2.setColor(new Color(120, 210, 102)); // Green
		 * g2.drawRect(g2Whalf - shapeWhalf, g2Hhalf - shapeHhalf, shapeW, shapeH);
		 * break;
		 * case HOSTILE:
		 * g2.setColor(new Color(255, 0, 0)); // Red
		 * int pX[] = { g2Whalf - shapeWhalf, g2Whalf, g2Whalf + shapeWhalf, g2Whalf, g2Whalf - shapeWhalf };
		 * int pY[] = { g2Hhalf, g2Hhalf - shapeHhalf, g2Hhalf, g2Hhalf + shapeHhalf, g2Hhalf };
		 * g2.drawPolyline(pX, pY, pX.length);
		 * break;
		 * case SELF:
		 * g2.setColor(new Color(69, 69, 69)); // Grey
		 * g2.drawOval(g2Whalf - shapeWhalf / 2, g2Hhalf - shapeHhalf / 2, shapeW / 2, shapeH / 2);
		 * 
		 * break;
		 * case UNKNOWN:
		 * default:
		 * // g2.setColor(new Color(242, 237, 90)); // Yellow
		 * 
		 * 
		 * break;
		 * } */
		if(this.projectile.getAltitude() == 1) g2.clearRect(g2Whalf - shapeWhalf - 1, g2Hhalf, shapeW + 2, shapeH - 1); // Clear bottom half
		// if (altitude == 0) then leave drawn shape intact
		if(this.projectile.getAltitude() == -1) g2.clearRect(g2Whalf - shapeWhalf - 1, g2Hhalf - shapeHhalf - 3, shapeW + 2, (shapeH / 2) + 4); // Clear top half

		return shape;
	}
}
